<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="关键词">
                <a-input v-model="queryParam.keyword" placeholder="请输入关键词" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="订单时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm:ss"
                  style="width: 100%"
                  @change="onChange"
                  @calendarChange="calendarChange"
                  :ranges="ranges"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="来源平台">
                <div class="select">
                  <a-select v-model="queryParam.platform" placeholder="请选择">
                    <a-select-option value="">全部</a-select-option>
                    <a-select-option value="0">微信小程序</a-select-option>
                  </a-select>
                  <a-button type="primary" @click="refresh">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <s-table
        ref="tableList"
        size="default"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        row-key="id"
        :columns="columns"
        :data="loadData"
      >
        <template slot="name" slot-scope="text, record">
          <a-row :gutter="20">
            <a-col span="5">
              <img
                :src="record.pic_list[0] || 'https://cyy-desc.oss-cn-beijing.aliyuncs.com/sp210329_163447.png'"
                style="width: 100%; border-radius: 5px"
              />
            </a-col>
            <a-col span="19">
              <a-tooltip>
                <template slot="title">
                  {{ record.goods_name }}
                </template>
                <div class="over_e">{{ record.goods_name }}</div>
              </a-tooltip>
              <div>{{ record.created_at }}</div>
            </a-col>
          </a-row>
        </template>
        <template slot="status" slot-scope="text, record">
          <a-badge v-if="record.is_hide == 0" status="success" />
          <a-badge v-else status="error" />
          <span>{{ record.is_hide | transWord }}</span>
        </template>
        <template slot="action" slot-scope="text, record">
          <a-button type="link" @click="orderReply(record.id)"> 回复 </a-button>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link" @click="(e) => e.preventDefault()"> 更多<a-icon type="down" /> </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-button v-if="record.is_hide == 1" type="link" @click="hideComment(record.id, 0)"> 显示 </a-button>
                <a-button v-else type="link" @click="hideComment(record.id, 1)"> 隐藏 </a-button>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm placement="left" @confirm="delComment(record.id, 1)">
                  <template slot="title">
                    <p>确认删除该评论吗？</p>
                  </template>
                  <a-button type="link"> 删除 </a-button>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
const columns = [
  {
    title: '商品ID',
    dataIndex: 'id',
    key: 'id',
    width: 100,
  },
  {
    title: '产品名称',
    dataIndex: 'name',
    key: 'name',
    width: 280,
    scopedSlots: { customRender: 'name' },
  },
  {
    title: '用户',
    dataIndex: 'nickname',
    key: 'nickname',
    width: 120,
  },
  {
    title: '评分',
    dataIndex: 'score',
    key: 'score',
    width: 120,
  },
  {
    title: '详情',
    dataIndex: 'content',
    key: 'content',
    width: 150,
    ellipsis: true,
  },
  {
    title: '状态',
    dataIndex: 'is_hide',
    key: 'is_hide',
    width: 120,
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 150,
    scopedSlots: { customRender: 'action' },
  },
]
import { STable } from '@/components'
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import moment from 'moment'
import { getComment, handleCommentHide, handleCommentDelete, handleCommentReply } from '@/api/order'
export default {
  name: 'OrderList',
  components: {
    STable,
  },
  data() {
    return {
      columns,
      selectedRowKeys: [],
      loadData: (parameter) => {
        Object.assign(parameter, {})
        return getComment(parameter).then((res) => {
          console.log(res, 'commont')
          return res.data
        })
      },
      ranges: {
        一月内: () => [moment().startOf('day').subtract(1, 'month'), moment()],
        昨天: () => [moment().startOf('day').subtract(1, 'days'), moment().endOf('day').subtract(1, 'days')],
        最近一周: () => [moment().startOf('day').subtract(1, 'weeks'), moment()],
      },
      locale: zhCN,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      queryParam: {},
      selectDate: {},
      totalCount: 0,
      reply: '',
    }
  },
  watch: {},
  created() {},
  activated() {},
  computed: {},
  filters: {
    transWord(val) {
      let str = ''
      if (val === '1') {
        str = '隐藏'
      }
      if (val === '0') {
        str = '显示'
      }
      return str
    },
  },
  methods: {
    moment,
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    refresh(row) {
      this.$nextTick(() => {
        this.$refs.tableList.refresh(true)
      })
    },
    calendarChange(date, dateString) {
      this.selectDate = date[0]
    },
    onChange(date, dateString) {
      this.queryParam.dateStart = dateString[0]
      this.queryParam.dateEnd = dateString[1]
    },
    hideComment(id, status) {
      const hide = this.$message.loading('提交中...', 0)
      handleCommentHide({
        id: id,
        status: status,
      }).then((res) => {
        hide()
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$nextTick(() => {
            this.$refs.tableList.refresh(true)
          })
        }
      })
    },
    delComment(id, status) {
      const hide = this.$message.loading('提交中...', 0)
      handleCommentDelete({
        id: id,
        status: status,
      }).then((res) => {
        hide()
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$nextTick(() => {
            this.$refs.tableList.refresh(true)
          })
        }
      })
    },
    orderReply(id) {
      const h = this.$createElement
      this.$confirm({
        icon: '<>',
        closable: true,
        width: 540,
        title: '回复内容',
        maskClosable: true,
        content: h('div', {}, [
          h('a-textarea', {
            props: {
              placeholder: '填写回复内容',
              autoSize: { minRows: 4, maxRows: 6 },
            },
            on: {
              change: (val) => {
                this.reply = val.target.value
              },
            },
          }),
        ]),
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          const hide = this.$message.loading('提交中...', 0)
          handleCommentReply({
            id: id,
            reply_content: this.reply,
          }).then((res) => {
            hide()
            if (res.code !== 0) {
              this.$error({
                content: res.msg,
              })
            } else {
              this.$nextTick(() => {
                this.$refs.tableList.refresh(true)
              })
            }
          })
        },
      })
    },
  },
}
</script>
<style lang="less" scoped>
.ant-btn-link {
  padding: 0;
}
/deep/.ant-card-head {
  border-bottom: none;
}
.over_e {
  width: 80%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
